package com.mozicoding.foundation.id;

public class UuidGenerator implements IdGenerator<String> {
    @Override
    public String generate() {
        return null;
    }

    /*
    public static void main(String[] args) {
        IdGenerator<String> generator = new UuidGenerator();
        String id = generator.generate();
        System.out.println( " id : " + id );

        BigDecimal base = BigDecimal.valueOf( 62 , 0 ) ;
        BigDecimal result = base.pow( 8 );
        System.out.println( result );
    }

    private final StringBuffer uuidBuffer = new StringBuffer();
    private final StringBuffer shortBuffer = new StringBuffer();

    private static char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's','t', 'u', 'v', 'w', 'x', 'y', 'z',
                                                    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                                                    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

    @Override
    public String generate() {
        shortBuffer.setLength(0);
        uuidBuffer.setLength(0);
        UUID uuid = UUID.randomUUID();
        uuidBuffer.append( uuid );

        int pos ;
        while( ( pos = uuidBuffer.indexOf( "-" ) ) != -1 ){
            uuidBuffer.deleteCharAt( pos );
        }

        System.out.println( "uuid : " + uuidBuffer );

        for (int i = 0; i < 8; i++) {
            // 每次循环从 uuid 字符缓冲区中截取 4 个字符
            String str = uuidBuffer.substring( i * 4 , i * 4 + 4 );
            System.out.print( "str : " + str );
            // 将截取到的4个字符按照16进制转换成十进制整数
            int x = Integer.parseInt(str, 16);
            System.out.println( " , x : " + x  );
            System.out.println( chars[ x % chars.length ] + " , " + Integer.toHexString( x ) );
            shortBuffer.append(chars[ x % chars.length] );
        }

        String id = shortBuffer.toString() ;

        return id ;
    }
    */

}
